package com.can.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.can.reggie.common.CustomException;
import com.can.reggie.common.R;
import com.can.reggie.dto.DishDto;
import com.can.reggie.entity.Dish;
import com.can.reggie.entity.DishFlavor;
import com.can.reggie.entity.SetmealDish;
import com.can.reggie.mapper.DishMapper;
import com.can.reggie.service.DishFlavorService;
import com.can.reggie.service.DishService;
import com.can.reggie.service.SetmealDishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.DeleteMapping;

import java.util.List;

@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 新增菜品,同时插入菜品对应的口味数据,需要操作两张表:dish和dish_flavor
     *
     * @param dishDto
     */
    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        //1.保存菜品的基本信息到菜品表dish
        this.save(dishDto);

        //2.先得到dish表中的id,添加到每一条口味中去,再保存口味表
        Long dishId = dishDto.getId();
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishId);
        }
        //保存口味到口味表
        dishFlavorService.saveBatch(flavors);
    }


    /**
     * 根据id查询菜品信息和对应的口味信息
     *
     * @param id
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {

        DishDto dishDto = new DishDto();

        //查询菜品基本信息,从dish表查询,拷贝进dishDto
        Dish dish = this.getById(id);
        BeanUtils.copyProperties(dish,dishDto);

        //查询当前菜品对应的口味信息,从dish_flavor表查询,拷贝进dishDto
        LambdaQueryWrapper<DishFlavor> lwq = new LambdaQueryWrapper();
        lwq.eq(DishFlavor::getDishId, id);
        List<DishFlavor> flavorList = dishFlavorService.list(lwq);
        dishDto.setFlavors(flavorList);

        return dishDto;
    }


    /**
     * 根据id修改菜品信息和对应的口味信息
     * @param dishDto
     */
    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {

        //1.更新dish表,菜品基本信息
        this.updateById(dishDto);

        //2.更新dish_flavor表,菜品口味信息
        //获取到dishId
        Long dishDtoId = dishDto.getId();
        //清理当前菜品对应口味数据---dish_flavor表的delete操作
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId,dishDtoId);
        dishFlavorService.remove(lqw);

        //添加当前提交过来的口味数据--dish_flavor表的insert操作
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishDtoId);
        }
        dishFlavorService.saveBatch(flavors);

    }


    /**
     * 根据id修改菜品状态
     *
     * @param ids
     * @param stateId
     */
    @Override
    public R<String> stateUpdate(Long[] ids, Integer stateId) {
        //是否有菜品关联套餐
        boolean flag = false;

        if (stateId == 0) {
            //停售操作
            //1.遍历ids数组
            for (Long id : ids) {
                //2.根据id判断套餐中是否含有此菜品
                LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<SetmealDish>();
                lqw.eq(SetmealDish::getDishId, id);
                int count = setmealDishService.count(lqw);

                if (count == 0) {
                    //3.套餐中无此菜品,则设置此菜品的状态为0
                    Dish dish = new Dish();
                    dish.setId(id);
                    dish.setStatus(0);
                    this.updateById(dish);
                }else {
                    //菜品被套餐关联
                    flag=true;
                }
            }
            //是否有菜品关联套餐
            if (flag){
                throw new CustomException("若菜品已关联套餐, 则不能更改");
            }
        }
        else {
            //启售操作
            //1.遍历ids数组
            for (Long id : ids) {
                //2.设置此菜品的状态为1
                Dish dish = new Dish();
                dish.setId(id);
                dish.setStatus(1);
                this.updateById(dish);
            }
        }

        //所有菜品状态更完成,返回R
        return R.success("售卖状态更改成功");
    }


    /**
     * 根据id更改菜品id_delete状态,和菜品口味表id_delete状态
     *
     * @param ids
     * @return
     */
    @Override
    public R<String> deleteDish(Long[] ids) {
        //是否有菜品的售卖状态为启售
        boolean flag = false;

        //遍历每一个ids
        for (Long id : ids) {
            //判断售卖状态是否为0
            LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<Dish>();
            lqw.eq(Dish::getId,id);
            lqw.eq(Dish::getStatus, 0);
            int count = this.count(lqw);

            if (count!=0){
                //售卖状态为停售,则把每菜品对应的setIs_deleted改为隐藏
                Dish dish = new Dish();
                dish.setId(id);
                dish.setIsDeleted(1);
                this.updateById(dish);

                //同时,更改菜品口味表id_delete状态
                DishFlavor dishFlavor = new DishFlavor();
                dishFlavor.setIsDeleted(1);

                LambdaUpdateWrapper<DishFlavor> luw = new LambdaUpdateWrapper();
                luw.eq(DishFlavor::getDishId,id).set(DishFlavor::getIsDeleted,1);
                dishFlavorService.update(luw);
            }else {
                //有菜品售卖状态为启售
                flag=true;
            }
        }
        if (flag){
             throw new CustomException("若售卖状态为启售, 则不能删除");
        }

        return R.success("删除成功");
    }

}
